import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Collections;
import java.util.Vector;

/**
 * @author zhangli
 * @version V1.0
 * @create 2019-09-09 15:41
 **/
public class JsonTest2 {
    public static void main(String[] args) throws ParseException {

        String content = "{\n" +
                "\t\"chart\": {\n" +
                "\t\t\"timestamp\": \"2019-10-15 15:24\",\n" +
                "\t\t\"beginTime\": \"2019-10-15 14:54\",\n" +
                "\t\t\"endTime\": \"2019-10-15 15:24\",\n" +
                "\t\t\"timeGranularity\": \" \",\n" +
                "\t\t\"dataset\": [{\n" +
                "\t\t\t\"head\": {\n" +
                "\t\t\t\t\"categories\": [{\n" +
                "\t\t\t\t\t\"id\": \"21\",\n" +
                "\t\t\t\t\t\"name\": \"callifapp_bak_133.174:17001\"\n" +
                "\t\t\t\t}],\n" +
                "\t\t\t\t\"serieses\": [{\n" +
                "\t\t\t\t\t\"id\": \"11\",\n" +
                "\t\t\t\t\t\"name\": \"Apdex\"\n" +
                "\t\t\t\t}],\n" +
                "\t\t\t\t\"rows\": [{\n" +
                "\t\t\t\t\t\"id\": \"1571122440000\",\n" +
                "\t\t\t\t\t\"name\": \"2019-10-15 14:54\"\n" +
                "\t\t\t\t}, {\n" +
                "\t\t\t\t\t\"id\": \"1571122500000\",\n" +
                "\t\t\t\t\t\"name\": \"2019-10-15 14:55\"\n" +
                "\t\t\t\t}, {\n" +
                "\t\t\t\t\t\"id\": \"1571122560000\",\n" +
                "\t\t\t\t\t\"name\": \"2019-10-15 14:56\"\n" +
                "\t\t\t\t}, {\n" +
                "\t\t\t\t\t\"id\": \"1571122620000\",\n" +
                "\t\t\t\t\t\"name\": \"2019-10-15 14:57\"\n" +
                "\t\t\t\t}],\n" +
                "\t\t\t\t\"cols\": [{\n" +
                "\t\t\t\t\t\"id\": \"nb.message.apdex\",\n" +
                "\t\t\t\t\t\"name\": \"Apdex\"\n" +
                "\t\t\t\t}, {\n" +
                "\t\t\t\t\t\"id\": \"nb.message.satisfied_count\",\n" +
                "\t\t\t\t\t\"name\": \"满意的次数\"\n" +
                "\t\t\t\t}, {\n" +
                "\t\t\t\t\t\"id\": \"nb.message.tolerable_count\",\n" +
                "\t\t\t\t\t\"name\": \"可忍受的次数\"\n" +
                "\t\t\t\t}, {\n" +
                "\t\t\t\t\t\"id\": \"nb.message.frustrated_count\",\n" +
                "\t\t\t\t\t\"name\": \"令人沮丧的次数\"\n" +
                "\t\t\t\t}, {\n" +
                "\t\t\t\t\t\"id\": \"nb.message.success.count\",\n" +
                "\t\t\t\t\t\"name\": \"成功访问次数\"\n" +
                "\t\t\t\t}]\n" +
                "\t\t\t},\n" +
                "\t\t\t\"data\": [\n" +
                "\t\t\t\t[\n" +
                "\t\t\t\t\t[\n" +
                "\t\t\t\t\t\t[1.0, 2, 0, 0, 2],\n" +
                "\t\t\t\t\t\t[1.0, 2, 0, 0, 2],\n" +
                "\t\t\t\t\t\t[1.0, 2, 0, 0, 2],\n" +
                "\t\t\t\t\t\t[1.0, 2, 0, 0, 2]\n" +
                "\t\t\t\t\t]\n" +
                "\t\t\t\t]\n" +
                "\t\t\t]\n" +
                "\t\t}]\n" +
                "\t}\n" +
                "}";
        JSONObject json = JSON.parseObject(content);
//        System.out.println(json);
        json = (JSONObject) json.get("chart");
        JSONArray jsonArray = (JSONArray) json.get("dataset");


        JSONArray jsonServices = jsonArray.getJSONObject(0).getJSONObject("head").getJSONArray("serieses");
        JSONArray jsonCols = jsonArray.getJSONObject(0).getJSONObject("head").getJSONArray("cols");
        JSONArray jsonRows = jsonArray.getJSONObject(0).getJSONObject("head").getJSONArray("rows");

        JSONArray jsonDatas = jsonArray.getJSONObject(0).getJSONArray("data").getJSONArray(0);

        JSONArray _addServices = new JSONArray();//封装成新的json对象
        JSONObject destJson = new JSONObject(true);//封装成新的json对象

//        for (Object jsonService : jsonServices) {
        for (int index = 0; index < jsonServices.size(); index++) {
            JSONObject _service = (JSONObject) jsonServices.get(index);
            JSONObject dest_Json = new JSONObject(true);
            JSONArray _addDatas = new JSONArray();

            JSONArray jy = (JSONArray) jsonDatas.get(index);
            for (int i = 0; i < jy.size(); i++) {
                JSONArray sub_jy = (JSONArray) jy.get(i);
                JSONObject sub_json = (JSONObject) jsonRows.get(i);

                JSONObject _destJson = new JSONObject(true);
                for (int i1 = 0; i1 < sub_jy.size(); i1++) {
                    String value = String.valueOf(sub_jy.get(i1));
                    JSONObject sub_cols = (JSONObject) jsonCols.get(i1);
                    String col_name = sub_cols.getString("name");
//                        System.out.println(value+"-"+col_name+"-"+sub_json.get("name"));

                    _destJson.put("time", sub_json.get("name"));
                    _destJson.put(col_name, value);
                }
                _addDatas.add(_destJson);

            }
            dest_Json.put("name", _service.get("name"));
            dest_Json.put("datas", _addDatas);
            _addServices.add(dest_Json);
            destJson.put("serieses", _addServices);
        }
        System.out.println(destJson);
        operation(destJson,"Apdex.成功访问次数");
    }

    public static void operation(JSONObject opJson ,String operator){
        Vector<String> opr = new Vector();
        char _charArray [] = operator.toCharArray();
        for (char c : _charArray) {
            if(equalChar(c)!=null){
                opr.add(equalChar(c));
            }
        }
        if(!opr.isEmpty()){
            operator =  operator.replaceAll("\\+","#").replaceAll("\\-","#");

            String [] sp = operator.split("#");
            Vector<String> src_Oprdata = new Vector<>();
            for (String _sp : sp) {
//            String [] sp_kv = _sp.split(".");
                src_Oprdata.add(_sp);

            }

            System.out.println(operator);
            Vector<Vector> coloction = new Vector<>();
            for (int i = 0; i < src_Oprdata.size(); i++) {
                Vector v = getDatas(src_Oprdata.get(i),opJson);
                coloction.add(v);
            }
            System.out.println(diguiOpr(coloction,opr));
            Vector<BigDecimal> destData = diguiOpr(coloction,opr);
            JSONArray jsonArray =  opJson.getJSONArray("serieses").getJSONObject(0).getJSONArray("datas");
            //写入kpi 文件的地方
            for (int i = 0; i < destData.size(); i++) {
                String time = jsonArray.getJSONObject(i).getString("time");
                String value = String.valueOf(destData.get(i));
                System.out.println("TIME = "+time+",VALUE="+value);
            }
        }else {
            Vector destData = getDatas(operator,opJson);
            System.out.println(destData);
            JSONArray jsonArray = opJson.getJSONArray("serieses").getJSONObject(0).getJSONArray("datas");
            for (int i = 0; i < destData.size(); i++) {
                String time = jsonArray.getJSONObject(i).getString("time");
                String value = String.valueOf(destData.get(i));
                System.out.println("TIME = "+time+",VALUE="+value);
            }
        }


    }

    private static Vector<BigDecimal> diguiOpr(Vector<Vector> vectors,Vector<String> opr){
        Vector<BigDecimal> _tmp = new Vector();
        Vector<BigDecimal> tmp = new Vector();
        for (int i = 0; i < vectors.size(); i++) {
            Vector<BigDecimal> dataV1  = vectors.get(i);
            if(i==0){
                _tmp = dataV1;
                continue;
            }

            String _opr = opr.get(i-1);
            for (int i1 = 0; i1 < _tmp.size(); i1++) {
                if(_opr.equals("+")){
                    double _value = _tmp.get(i1).doubleValue()+dataV1.get(i1).doubleValue();
                    tmp.add(new BigDecimal(_value));
                }else {
                    double _value = _tmp.get(i1).doubleValue()-dataV1.get(i1).doubleValue();
                    tmp.add(new BigDecimal(_value));
                }

            }
            Collections.copy(_tmp,tmp);
            tmp.clear();

            if(i==opr.size()){
                return _tmp;
            }

        }
        return _tmp;

    }


    private static Vector<BigDecimal> getDatas(String src_Oprdata,JSONObject dataJson){
        Vector<BigDecimal> _newVector =new Vector<>();
        JSONArray jsonArray = dataJson.getJSONArray("serieses");
        String _sv[] = src_Oprdata.split("\\.");
        for (Object obj : jsonArray) {
            JSONObject _json = (JSONObject)obj;
            if(!_json.getString("name").equalsIgnoreCase(_sv[0])){
                continue;
            }
           JSONArray datas =  _json.getJSONArray("datas");
            for (Object data : datas) {
                JSONObject json_data =(JSONObject)data;
                _newVector.add(json_data.getBigDecimal(_sv[1]));

            }
        }
        return _newVector;

    }
    public static String equalChar(char _char){
        if('+'==_char){
            return "+";
        }else if('-'==_char) {
            return "-";
        }else {
            return null;
        }

    }


}
